<?php
namespace app\admin\controller;
class Recharge extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['pay_status']!='') $map[] = ['and','a.pay_status','=',$d['pay_status']];
            if($d['keyword']!='') $map[] = ['and','b.username|a.order_no','like',"%{$d['keyword']}%"];
            if($d['pay_type']!='') $map[] = ['and','a.pay_type','=',$d['pay_type']];
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $rs = $this->db->query("select a.*,b.username from recharge a
                    left join user b on a.user_id=b.id
                   $w[0] order by a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'pay_status' => function($v){return c("type.pay_status.{$v}");},
                    'pay_type' => function($v){return c("type.pay_type.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $info = $this->db->find("recharge",$id);
        $rs = $this->db->delete("recharge",$id);
        if($rs){
            m('Log','xcom')->record($this->userinfo['id'],"删除了用户ID[{$info['user_id']}]的充值记录|金额:{$info['money']}|支付状态{$info['pay_status']}",'admin_user');
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $info = $this->db->find("recharge",$id);
                $this->db->delete('recharge',$id);
                m('Log','xcom')->record($this->userinfo['id'],"删除了用户ID[{$info['user_id']}]的充值记录|金额:{$info['money']}|支付状态{$info['pay_status']}",'admin_user');
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }


}
